package com.topvision.elementanim.hualang;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.widget.FrameLayout;

import com.topvision.elementanim.BitmapUtils;
import com.topvision.elementanim.FastBlur;

import java.io.File;

/**
 * Author: lucy
 * Comment: //TODO
 * Date: 2017-09-30 09:47
 */

public class LoadBlurAsyncTask extends AsyncTask<File, Void, Bitmap> {

    private final FrameLayout mContainer;
    private final PageChangeInterface mPageChangeInterface;

    public LoadBlurAsyncTask(FrameLayout container, PageChangeInterface pageChangeInterface) {
        mContainer = container;
        mPageChangeInterface = pageChangeInterface;
    }

    @Override
    protected Bitmap doInBackground(File... params) {
        //这是在后台子线程中执行的
        if (params[0].getName().endsWith(".mp4")) {
            Bitmap videoThumbnail = VideoThumbnailUtils.getLocalVideoThumbnail(params[0].getAbsolutePath());
            return FastBlur.doBlur(BitmapUtils.getBitmap(videoThumbnail, 0.01f), 60, false);
        } else {
            System.out.println(params[0].getName());
            return FastBlur.doBlur(BitmapUtils.getBitmap(params[0], 60), 60, false);
        }
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        //当任务执行完成是调用,在UI线程
        mContainer.setBackground(new BitmapDrawable(bitmap));
        if (mPageChangeInterface!=null)
            mPageChangeInterface.onPageChange();
    }
}
